odoo.define('field_image_preview.image_widget_extend', function (require) {
    "use strict";

    var imageWidget = require('web.basic_fields').FieldBinaryImage;
    var DocumentViewer = require('mail.DocumentViewer');
    var field_utils = require('web.field_utils');
    var fieldRegistry = require('web.field_registry');
    var imagePreviewField = imageWidget.extend({

        _renderReadonly: function () {
            this._super.apply(this, arguments);
            var self = this;
            this.$("img").css("cursor","pointer")
            this.$("img").click(function (e) {
                console.log(self);
                var name_field = self.name;
                var source_id = self.model + "/" + JSON.stringify(self.res_id) + "/" + name_field
                    + "?unique=" + field_utils.format.datetime(self.recordData.__last_update).replace(/[^0-9]/g, '') + "#";
                var attachments = [{
                    "filename": self.recordData.display_name,
                    "id": source_id,
                    "is_main": true,
                    "mimetype": "image/jpeg",
                    "name": self.recordData.display_name + " " + self.value,
                    "type": "image",
                }];
                var attachmentViewer = new DocumentViewer(self, attachments, source_id);
                attachmentViewer.appendTo($('body'));
            });
        },
    });
    fieldRegistry.add('image_preview', imagePreviewField);
    return {
        imagePreviewField: imagePreviewField,
    };

});